package com.jnu.booklist.data;

import android.content.Context;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class DataBank {
    private final Context context;
    List<Book>bookItemsList;

    public DataBank(Context context) {
        this.context=context;
    }

    public List<Book> loadData() {
        bookItemsList=new ArrayList<>();
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(context.openFileInput("Serializable.txt"));
            bookItemsList = (ArrayList<Book>) objectInputStream.readObject();
        }catch (Exception e){
            e.printStackTrace();
        }
        return bookItemsList;
    }

    public void saveData() {
        ObjectOutputStream objectOutputStream=null;
        try{
            objectOutputStream=new ObjectOutputStream(context.openFileOutput("Serializable.txt", Context.MODE_PRIVATE));
            objectOutputStream.writeObject(bookItemsList);

        }catch(IOException e){
            e.printStackTrace();
        }finally {
            try {
                if (objectOutputStream != null) {
                    objectOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
